//
//  BaseTabBarViewController.m
//  BMTC
//
//  Created by Eternity on 16/4/29.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "HomePageViewController.h"
#import "MakeMoneyViewController.h"
#import "TheTransferViewController.h"
#import "MyViewController.h"

#import "TabBarButton.h"
#import "BaseViewController.h"



@interface BaseTabBarViewController ()

@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createViewControllers];
    [self createTabBarItems];

    
    
}


- (void)createViewControllers {
    
    NSArray *arrViewControllers = @[@"HomePageViewController",@"MakeMoneyViewController",@"TheTransferViewController",@"MyViewController"];
//    NSArray *arrSubViewControllers = @[@"OneViewController",@"TwoViewController",@"ThreeViewController",@"FourViewController",@"FiveViewController",@"SixViewController"];
//    NSArray *arrSubTitles = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six"];
    
//    NSMutableArray *ArrSub = [NSMutableArray array];
//    
    NSMutableArray *navControllers = [NSMutableArray array];
//
//    for (NSInteger index = 0; index < arrSubViewControllers.count; index ++ ) {
//        Class class = NSClassFromString(arrSubViewControllers[index]);
//        
//        BaseSubViewController *subViewControllers = [[class alloc] init];
//        CGFloat R = arc4random() % 255 / 256.0;
//        CGFloat G = arc4random() % 255 / 256.0;
//        CGFloat B = arc4random() % 255 / 256.0;
//        subViewControllers.view.backgroundColor = [UIColor colorWithRed:R green:G blue:B alpha:1];
//        [ArrSub addObject:subViewControllers];
//        
//    }
    
    for (NSInteger index = 0 ; index < arrViewControllers.count; index ++ ) {
        Class class = NSClassFromString(arrViewControllers[index]);
        
        UINavigationController *nav = nil;
        if (index == 5) {
//            FirstViewController *first = [[class alloc] initWithSubTitles:arrSubTitles addViewControllers:ArrSub];
//            nav = [[UINavigationController alloc] initWithRootViewController:first];
        } else {
            
            BaseViewController * base = [[class alloc] init];
            nav = [[UINavigationController alloc] initWithRootViewController:base];
            
        }
        nav.navigationBar.barTintColor = RGBA(122, 189, 234, 1);
        nav.navigationBar.translucent = NO;
        [navControllers addObject:nav];
    }
    self.viewControllers = navControllers;
    self.tabBar.translucent = NO;
    
}




- (void)createTabBarItems {
    
    // 每个tabbar的名字
    NSArray *itemName = @[@"首页",@"我的项目",@"债权转让",@"我的"];
    // 没点击的图片
    NSArray *itemNormalImage = @[@"首页",@"项目 (2)",@"债权转让",@"我的"];
    // 设置选择的图片
    NSArray *itemSelectImage = @[@"首页 (1)",@"项目 (3)",@"债权转让 (1)",@"我的 (1)"];
    
    // 遍历四个tabbar的名字数组
    for (NSInteger index = 0;index < itemName.count; index ++) {
        // 创建四个button就是tabbar的item 在继承于Button的LJItem的类里面写了字的位置和图片的位置
        UIButton *item = [TabBarButton buttonWithType:UIButtonTypeCustom];
        // 设置每个button的位置
        item.frame = CGRectMake(index * WIDTH / 4.0 , 0, WIDTH / 4.0, 49);
        // 字体大小
        item.titleLabel.font = [UIFont systemFontOfSize:10];
        // 字体位置
        item.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 设置字没选中时的颜色
        [item setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        // 设置字选中时的颜色
        [item setTitleColor:RGBA(122, 189, 234, 1) forState:UIControlStateSelected];
        // 设置字
        [item setTitle:itemName[index] forState:UIControlStateNormal];
        // 设置图片
        [item setImage:[[UIImage imageNamed:itemNormalImage[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        // 设置选中的图片
        [item setImage:[[UIImage imageNamed:itemSelectImage[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
        // 设置button的点击方法
        [item addTarget:self action:@selector(itemClicked:) forControlEvents:UIControlEventTouchUpInside];
        // 通过tag值找到对应的button
        item.tag = 400 + index;
        //如果tag为400 那就是选中状态
        if (item.tag == 400) {
            item.selected = YES;
        }
        // 把button添加到tabbar上
        [self.tabBar addSubview:item];
    }
}

/** button的点击方法  */
- (void) itemClicked:(UIButton *)sender {
    // 首先遍历viewControllers的个数
    for (NSInteger index = 0; index < self.viewControllers.count; index ++) {
        //其他的设置为no
        UIButton *item = (UIButton *)[self.tabBar viewWithTag:400 + index];
        item.selected = NO;
    }
    //把点击的设置成yes
    sender.selected = YES;
    // 通过tag找到点击的是哪个index
    self.selectedIndex = sender.tag - 400;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
